using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;

namespace Netduino4Fun.WebServer.Test
{
	public class RGBLed
	{
		private PWM R;
		private PWM G;
		private PWM B;
		public bool IsEnable;

		public RGBLed(Cpu.Pin pinR, Cpu.Pin pinG, Cpu.Pin pinB)
		{
			R = new PWM(pinR);
			G = new PWM(pinG);
			B = new PWM(pinB);
			ChangeColor(0, 0, 0);
			IsEnable = true;
		}

		public void Start()
		{
			IsEnable = true;
		}

		public void Stop()
		{
			ChangeColor(0, 0, 0);
			IsEnable = false;
		}

		public void ChangeColor(uint r, uint g, uint b)
		{
			if (!IsEnable) return;
			var pR = RgbToPwm(r);
			var pG = RgbToPwm(g);
			var pB = RgbToPwm(b);
			Debug.Print("r=" + pR + "% g=" + pG + "% b=" + pB + "%");
			R.SetDutyCycle(pR);
			G.SetDutyCycle(pG);
			B.SetDutyCycle(pB);
			//Debug.Print(rep.ToString());
		}

		public uint RgbToPwm(uint color)
		{
			if (color == 0)
				return color;
			var rep = color * 100 / 255;
			return rep;
		}

	}
}